<?php
    $this->load->view("fungsiPenting.php");
?>
<div class="container" style="margin-top:40px">
	<div class="col-md-offset-1">
	<div class="col-md-11">
	<div class="form-horizontal">
		<div class="panel panel-default">
			<div class="panel-heading">
			<h4 class="panel-title"><b> Report Filter</b></h4>
			</div>
			<div class="panel-body">	
				<div class="row">
					<form method="post" accept-charset="utf-8" />
					<div class="col-sm-12">
						
							<div class="col-sm-3 form-group ">
									<div class="input-group">
										<span class="input-group-addon">From</span>
										<input type="text" class="form-control text-center input-sm" name="date_start" id="datepicker1" value="<?php echo $date_start_set; ?>">
									</div>
								
							</div>
							<div class="col-sm-3 form-group ">
									<div class="input-group">
										<span class="input-group-addon">To</span>
										<input type="text" class="form-control text-center input-sm" name="date_end" id="datepicker2" value="<?php echo $date_end_set; ?>">
									</div>
								
							</div>
							<div class="col-sm-3 form-group ">
								<div class="btn-group">
									
										<button type="submit" class="form-group btn btn-default btn-sm" >Retrieve</button>
									
								</div>
							</div>
							
						
					</div>
					<form>
				</div>
				
			</div>
		</div>
	</div>
	</div>
	</div>
	<?php if($table2 != null) { ?>
	<div class="col-md-offset-1">
	<div class="col-md-11">
	<div id="report" class="panel panel-default">
		<div class="panel-heading">
		<h4 class="panel-title"><b> Reports Summary List</b></h4>
		
		<div class="btn-group pull-right">
			<button class="btn btn-default btn-sm" id="btnExport"><i class="icon-file-excel on-left"></i> Excel</button>
			<button class="btn btn-default btn-sm" type='button' value='Print' onclick="printDiv('tblExport');"><i class="icon-printer on-left"></i> Print</button>
		</div>
		
		</div>
		<div class="panel-body" id="tblExport">	
			<div class="row">
				<div class="col-sm-4">
					<img class="img-responsive" src="<?php echo base_url() ?>static/images/logo_telkomsel.png">
				</div>
				<div class="col-sm-4" align="center">
					<!--PT SANGGARCIPTA KREASITAMA<br />
					MANAGEMENT OFFICE<br />
					JL JEND GATOT SUBROTO NO.42 JAKARTA 12170 - INDONESIA<br />
					TELP: 3000 2008 FAX 3000 2009<br />-->
					<h1>Overtime</h1>
				</div>
				<div class="col-sm-4"></div>
			</div>
			<br />
			
			<div class="row">
				<div class="col-sm-12">
					<div>
						<table class="table">
							<tr>
								<td width="5%" style="background-color:#eee;">Periode</td>
								<td><?php echo tgl_indo($date_start_set)." - ".tgl_indo($date_end_set);?></td>
							</tr>
						</table>
					</div>
					
					<div class="table-responsive">
						<table class="table">
							<tr>
								<td colspan="11">
									<table class="table" style="margin:0 auto;">
										<tr style="border:none;">
											<td width="130px" style="border:none;" colspan="3"><label class="control-label">Work Description</label></td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<th style="text-align:center;" rowspan="2">No.</th>
								<th style="text-align:center;" rowspan="2">Information</th>
								<!--<th style="text-align:center;" rowspan="2">Requestor</th>
								<th style="text-align:center;" rowspan="2">Request No</th>
								<th style="text-align:center;" rowspan="2">Request Date</th>
								<th style="text-align:center;" rowspan="2">Floor</th>
								<th style="text-align:center;" rowspan="2">Complex</th>-->
								<th style="text-align:center;" colspan="2">Order</th>
								<th style="text-align:center;" rowspan="2">Price<br />(US$)</th>
								<th style="text-align:center;" rowspan="2">Total<br />(Hour)</th>
								<th style="text-align:center;" rowspan="2">Price</th>
								<!--<th style="text-align:center;" rowspan="2">#</th>-->
							</tr>
							<tr>
								<th style="text-align:center;">Date</th>
								<th style="text-align:center;">Time</th>
							</tr>
							<?php
								if($table2 != null)
								{
									$i = 0; // numbering per document id
									$holiday_overtime = false; // Fitur khusus untuk lembur yang ingin dihitung tanpa melihat kondisi & syarat lembur terhadap hari tersebut
									
									// Variable untuk kondisi jam start overtime
									$ohs_wekd = 18; // overtime hour set weekday
									$ohs_satd = 13; // overtime hour set weekend : saturday
									$ohs_sund = 00; // overtime hour set weekend : sunday
									
									$str_ohs_wekd = str_pad($ohs_wekd, 2, "0", STR_PAD_LEFT);
									$str_ohs_satd = str_pad($ohs_satd, 2, "0", STR_PAD_LEFT);
									$str_ohs_sund = str_pad($ohs_sund, 2, "0", STR_PAD_LEFT);
									
									foreach($table2 as $data2)
									{
										$i++;
										
										// variable untuk mendapatkan jumlah range hari dari start date hingga end date
										$date_s = date('d-m-Y', strtotime($data2['SO_SDATE']));
										$date_e = date('d-m-Y', strtotime($data2['SO_EDATE']));
										$datetime1 = new DateTime($date_s);
										$datetime2 = new DateTime($date_e);
										$interval = date_diff($datetime1, $datetime2);
										$diff = $interval->format('%a');
										$diff = $diff + 1; // +1 modifier agar jumlah hari nya match, ex: 01-01-2014 - 03-01-2014 = (3-1)+1 = 3
										
										$date_set = $date_s ;
										$grandtotal_hour = 0;
										$hour_wrong_input_by_overtime_day = ""; // informasi utk user yang salah input jam di hari pertama overtime
										
										for($x = 1; $x <= $diff; $x++)
										{
											$hour_display = "";
											$hour_total = "0";
											$day_by_week = date('N',strtotime($date_set)); // 1-7 : Monday - Sunday
											
											if($x == 1) // data pertama
											{
												if($day_by_week >= 1 && $day_by_week <= 5) // weekday
												{
													if(intval($data2['SO_SHOUR']) >= $ohs_wekd )
													{
														$hour_display = $data2['SO_SHOUR'].":00"." - ".($diff > 1 ? "24" : $data2['SO_EHOUR']).":00";
														$hour_total = ($diff > 1 ? 24 : $data2['SO_EHOUR']) - $data2['SO_SHOUR'];
													}
													else
													{
														$hour_display = $str_ohs_wekd.":00"." - ".($diff > 1 ? "24" : $data2['SO_EHOUR']).":00";
														$hour_total = ($diff > 1 ? 24 : $data2['SO_EHOUR']) - $ohs_wekd;
														$hour_wrong_input_by_overtime_day = $data2['SO_SHOUR'].":00";
													}
												}
												else if($day_by_week == 6) // weekend : sabtu
												{
													if(intval($data2['SO_SHOUR']) >= $ohs_satd )
													{
														$hour_display = $data2['SO_SHOUR'].":00"." - ".($diff > 1 ? "24" : $data2['SO_EHOUR']).":00";
														$hour_total = ($diff > 1 ? 24 : $data2['SO_EHOUR']) - $data2['SO_SHOUR'];
													}
													else
													{
														$hour_display = $str_ohs_satd.":00"." - ".($diff > 1 ? "24" : $data2['SO_EHOUR']).":00";
														$hour_total = ($diff > 1 ? 24 : $data2['SO_EHOUR']) - $ohs_satd;
														$hour_wrong_input_by_overtime_day = $data2['SO_SHOUR'].":00";
													}
												}
												else if($day_by_week == 7) // weekend : minggu
												{
													if(intval($data2['SO_SHOUR']) >= $ohs_sund )
													{
														$hour_display = $data2['SO_SHOUR'].":00"." - ".($diff > 1 ? "24" : $data2['SO_EHOUR']).":00";
														$hour_total = ($diff > 1 ? 24 : $data2['SO_EHOUR']) - $data2['SO_SHOUR'];
													}
													else
													{
														$hour_display = $str_ohs_sund.":00"." - ".($diff > 1 ? "24" : $data2['SO_EHOUR']).":00";
														$hour_total = ($diff > 1 ? 24 : $data2['SO_EHOUR']) - $ohs_sund;
														$hour_wrong_input_by_overtime_day = $data2['SO_SHOUR'].":00";
													}
												}
											}
											else if($x == $diff) // data akhir
											{
												if($diff >= 2) // syarat untuk data akhir, jumlah data minimal 2
												{
													if($day_by_week >= 1 && $day_by_week <= 5) // weekday
													{
														$hour_display = $str_ohs_wekd.":00"." - ".($data2['SO_EHOUR'] < $ohs_wekd ? $str_ohs_wekd : $data2['SO_EHOUR']).":00";
														$hour_total = ($data2['SO_EHOUR'] < $ohs_wekd ? $ohs_wekd : $data2['SO_EHOUR']) - $ohs_wekd;
													}
													else if($day_by_week == 6) // weekend : sabtu
													{
														$hour_display = $str_ohs_satd.":00"." - ".($data2['SO_EHOUR'] < $ohs_satd ? $str_ohs_satd : $data2['SO_EHOUR']).":00";
														$hour_total = ($data2['SO_EHOUR'] < $ohs_satd ? $ohs_satd : $data2['SO_EHOUR']) - $ohs_satd;
													}
													else if($day_by_week == 7) // weekend : minggu
													{
														$hour_display = $str_ohs_sund.":00"." - ".($data2['SO_EHOUR'] < $ohs_sund ? $str_ohs_sund : $data2['SO_EHOUR']).":00";
														$hour_total = ($data2['SO_EHOUR'] < $ohs_sund ? $ohs_sund : $data2['SO_EHOUR']) - $ohs_sund;
													}
												}
											}
											else // data tengah
											{
												if($diff >= 3) // syarat untuk data tengah, jumlah data minimal 3
												{
													if($day_by_week >= 1 && $day_by_week <= 5) // weekday
													{
														$hour_display = $str_ohs_wekd.":00"." - "."24".":00";
														$hour_total = 24 - $ohs_wekd;
													}
													else if($day_by_week == 6) // weekend : sabtu
													{
														$hour_display = $str_ohs_satd.":00"." - "."24".":00";
														$hour_total = 24 - $ohs_satd;
													}
													else if($day_by_week == 7) // weekend : minggu
													{
														$hour_display = $str_ohs_sund.":00"." - "."24".":00";
														$hour_total = 24 - 0;
													}
												}
											}
											
											$grandtotal_hour = $grandtotal_hour + $hour_total;
											
											echo "<tr>";
											
											if($x == 1) { 
                                                ?><td rowspan='<?php echo $diff;?>' style='vertical-align:top;' align='center'><?php echo $i;?></td><?php  
                                            } else { 
                                                
                                            }
											if($x == 1) 
											{ 
												echo 	"<td rowspan='".$diff."' style='vertical-align:top;' align='center'>"
														."<table border='0' cellspacing='1' cellpadding='1' style='border:none;'>"
														."<tr style='border:none;'><td style='border:none; width:130px;'>Request No</td><td style='border:none;'>&nbsp;:&nbsp;</td><td style='border:none;'>"
															."<a id='non-printable' class='no-print' href='".base_url()."reports/overtime/".urlencode(urlencode($data2['SO_ID']))."' target='_blank' >".$data2['SO_ID']."</a>"."<span id='printable'>".$data2['SO_ID']."</span>"
														."</td></tr>"
														."<tr style='border:none;'><td style='border:none;'>Request Date</td><td style='border:none;'>&nbsp;:&nbsp;</td><td style='border:none;'>".$data2['DTMUPD']."</td></tr>"
														."<tr style='border:none;'><td style='border:none;'>Requestor ID</td><td style='border:none;'>&nbsp;:&nbsp;</td><td style='border:none;'>".$data2['SO_NIP']."</td></tr>"
														."<tr style='border:none;'><td style='border:none;'>Requestor Name</td><td style='border:none;'>&nbsp;:&nbsp;</td><td style='border:none;'>".$data2['SO_NAME']."</td></tr>"
														."<tr style='border:none;'><td style='border:none;'>Floor</td><td style='border:none;'>&nbsp;:&nbsp;</td><td style='border:none;'>".$data2['SOD_FLOOR']."</td></tr>"
														."<tr style='border:none;'><td style='border:none;'>Complex</td><td style='border:none;'>&nbsp;:&nbsp;</td><td style='border:none;'>".$data2['PRICE_DESC']."</td></tr>"
														."</table>"
														."</td>"
														; 
											} else { }
											
											// if($x == 1) { echo "<td style='vertical-align:top;' align='center'>".$i."</td>"; } else { echo "<td></td>";}
											// if($x == 1) { echo "<td style='vertical-align:top;' align='left'>".$data2['SO_NIP']."<br />".$data2['SO_NAME']."</td>"; } else { echo "<td></td>";}
											// if($x == 1) 
											// { 
												// echo "<td style='vertical-align:top;'  align='center'>"
													// ."<a id='non-printable' class='no-print' href='".base_url()."reports/overtime/".urlencode(urlencode($data2['SO_ID']))."' target='_blank' >"
													// .$data2['SO_ID']
													// ."</a>"
													// ."<span id='printable'>".$data2['SO_ID']."</span>"
													// ."</td>"
													// ; 
											// } else { echo "<td></td>";}
											// if($x == 1) { echo "<td style='vertical-align:top; width:82px' align='left'>".$data2['DTMUPD']."</td>"; } else { echo "<td></td>";}
											// if($x == 1) { echo "<td style='vertical-align:top;' align='center'>".$data2['SOD_FLOOR']."</td>"; } else { echo "<td></td>";}
											// if($x == 1) { echo "<td style='vertical-align:top;' align='left'>".$data2['PRICE_DESC']."</td>"; } else { echo "<td></td>";}
											echo "<td style='vertical-align:top; width:82px;' align='left'>".$date_set."<br />".date('l',strtotime($date_set))."</td>";
											echo "<td style='vertical-align:top; width:94px;' align='left'>".$hour_display.($hour_wrong_input_by_overtime_day != "" && $x == 1 ? "<br /><span style='color:red;'>".$hour_wrong_input_by_overtime_day."</span>" : "")."</td>";
											echo "<td style='vertical-align:top;' align='center'>".$data2['PRICE']."</td>";
											echo "<td style='vertical-align:top; ".($hour_total <= 0 ? "color:red;" : "")."' align='center'>".$hour_total."</td>";
											echo "<td style='vertical-align:top;' align='center'>".$data2['SUBTOTAL']."</td>";
											echo "</tr>";
											
											$date_set = date('d-m-Y', strtotime($date_set . "+1 day"));
										}
										
										// Untuk display grand total total per document id
										echo "<tr>";
										echo "<td style='vertical-align:top; background-color:#dddddd;' colspan='2'></td>";
										echo "<td style='vertical-align:top; background-color:#eeeeee; font-size:15pt;' colspan='2' align='center'>".$diff."</td>";
										echo "<td style='vertical-align:top; background-color:#eeeeee; font-size:15pt;' align='center'>"."0"."</td>";
										echo "<td style='vertical-align:top; background-color:#eeeeee; font-size:15pt; ".($grandtotal_hour <= 0 ? "color:red;" : "")."' align='center'>".$grandtotal_hour."</td>";
										echo "<td style='vertical-align:top; background-color:#eeeeee; font-size:15pt;' align='center'>"."0"."</td>";
										echo "</tr>";
										
									}
								}
							?>
						</table>
					</div>
					<div style="page-break-inside:avoid;">
						Jakarta, <?php echo tgl_indo(date('d-m-Y'));?><br />
						<br />
						<br />
						<br />
						<br />
						<br />
						Manager
					</div>
				</div>
			</div>
			
		</div>
	</div>
	</div>
	</div>
	<?php } ?>
</div>
</div>

<script>
$(function() {
    $("input[id^='datepicker']").datetimepicker({
		scrollInput: false,
		yearOffset:0,
		lang:'en',
		timepicker:false,
		datepicker:true,
		format:'d-m-Y',
		formatDate:'d/m/Y',
		/*minDate:'-1970/01/01',
		onGenerate:function( ct ){
			$(this).find('.xdsoft_date.xdsoft_weekend')
				.addClass('xdsoft_disabled');
		}*/
	});
});

</script>
<script>
	$(function() {
		$("input[id^='datepicker']").datetimepicker({
			scrollInput: false,
			yearOffset:0,
			lang:'en',
			timepicker:false,
			datepicker:true,
			format:'d-m-Y',
			formatDate:'d/m/Y',
			/*minDate:'-1970/01/01',
			onGenerate:function( ct ){
				$(this).find('.xdsoft_date.xdsoft_weekend')
					.addClass('xdsoft_disabled');
			}*/
		});
	});
</script>
<script>
    $(document).ready(function () {
        $("#btnExport").click(function () {
            $("#tblExport").btechco_excelexport({
                containerid: "tblExport"
               , datatype: $datatype.Table
               , filename: 'Report_Overtime_<?php echo str_replace("-","",$this->input->post('date_start'))."-".str_replace("-","",$this->input->post('date_end')); ?>'
            });
			
        });
		
    });
</script>
<script>
	function printDiv(divName) 
	{
		var printContents = document.getElementById(divName).innerHTML;     
		var originalContents = document.body.innerHTML;       
		document.body.innerHTML = printContents;      
		window.print();      
		document.body.innerHTML = originalContents;
		//window.parent.reload();
   }
</script>
<style type="text/css">
table { border-collapse:collapse; width:100%;}
table td, table th { border:1px solid #ddd; padding:2px; }

table { page-break-inside:auto }
tr    { page-break-inside:avoid; page-break-after:auto }
thead { display:table-header-group }
tfoot { display:table-footer-group }

.table > tbody > tr > td {
	padding: 3px;
}
</style>